/**
 * 
 */
package com.java.service.impl;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.java.entity.User;
import com.java.mapper.UserMapper;
import com.java.utils.CookieConstantTable;
import com.java.utils.CookieUtils;

/**
* @author Kidney
* 创建时间：2019年2月25日 下午6:14:40
* Description:
*/
/**
 * @author KIDNEY
 *
 */

@Service("UserServiceImpl")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper um;
	
	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#deleteByPrimaryKey(java.lang.Integer)
	 */
	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return um.deleteByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#insert(com.java.entity.User)
	 */
	@Override
	public int insert(User record) {
		// TODO Auto-generated method stub
		return um.insert(record);
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#insertSelective(com.java.entity.User)
	 */
	@Override
	public int insertSelective(User record) {
		// TODO Auto-generated method stub
		return um.insertSelective(record);
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#selectByPrimaryKey(java.lang.Integer)
	 */
	@Override
	public User selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return um.selectByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#updateByPrimaryKeySelective(com.java.entity.User)
	 */
	@Override
	public int updateByPrimaryKeySelective(User record) {
		

		
		return um.updateByPrimaryKeySelective(record);
	

		
		
	
	
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#updateByPrimaryKey(com.java.entity.User)
	 */
	@Override
	public int updateByPrimaryKey(User record) {
		// TODO Auto-generated method stub
		return um.updateByPrimaryKey(record);
	}
	
	
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		//从session中获取用户详情
		User user = (User) request.getSession().getAttribute("username");

		//清除session和用于自动登录的cookie
		request.getSession().removeAttribute("username");
		

	}
	
	
	
	public User login(User user,HttpServletResponse response) {
		User result = new User();
		// 如果用户名和密码不为空，执行登录
		if (StringUtils.isNotBlank(user.getId().toString()) && StringUtils.isNotBlank(user.getPassword()) &&  um.selectByPrimaryKey(user.getId()).getPassword().equals(user.getPassword())==true  ) {
			
			result = um.selectByPrimaryKey(user.getId());
			
			Cookie keyCookie = new Cookie("username", user.getId().toString());

			CookieUtils.addCookie(response, keyCookie);

		}
		else{
			result=null;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#selectAll()
	 */
	@Override
	public List<User> selectAll() {
		// TODO Auto-generated method stub
		return um.selectAll();
	}

	/* (non-Javadoc)
	 * @see com.java.service.impl.UserService#selectByCondition(com.java.entity.User)
	 */
	@Override
	public List<User> selectByCondition(User record) {
		// TODO Auto-generated method stub
		return um.selectByCondition(record);
	}

	

}
